#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char ** argv)
{
  int i;
  int fd;
  char * mem;
  char * buff = "xiaozhiaaaa";

  // open /dev/mem with read and write mdoe
  if ((fd = open("/dev/mem", O_RDWR)) < 0) {
    perror("open error");
    return -1;
  }

  // map physical memory 0-10 bytes
  mem = mmap(0, 12, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (mem == MAP_FAILED) {
    perror("mmap error:");
    return -1;
  }

  // read old value
  for (i = 0; i < 10; i++)
    printf("\nold mem[%d]:%d", i, mem[i]);

  printf("the value is 0x%x\n", * ((int *) mem));

  //write memory
  memcpy(mem, buff, 7);

  // read new value
  for (i = 0; i < 10; i++)
    printf("\nnew mem[%d]:%c", i, mem[i]);

  printf("\n");
  munmap(mem, 10);
  close(fd);

  return 0;
}
